package hut.lvming.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import hut.lvming.pojo.User;
import hut.lvming.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
public class UserController {
    @Autowired(required = false)
    private UserService userService;

    @RequestMapping("/findAll")
    public String findAll(HttpServletResponse response) throws IOException {
        response.setContentType("text/json;charset=utf-8");
        PrintWriter printWriter=response.getWriter();
        ObjectMapper objectMapper=new ObjectMapper();
        String json=objectMapper.writeValueAsString(userService.findAll());
        printWriter.println(json);
        printWriter.flush();
        printWriter.close();
        return "allUser";
    }

    @RequestMapping("jqueryTest")
    public String toJqueryTest(){
        return "index";
    }

    @RequestMapping("/jQueryFun")
    public String jQueryFun(){
        return "jQueryFun";
    }

    @RequestMapping("/findUserById")
    public void findUserById(HttpServletResponse response,Integer uid) throws IOException {
        User user=userService.findUserById(uid);
        response.setContentType("text/json;charset=utf-8");
        PrintWriter printWriter=response.getWriter();
        ObjectMapper objectMapper=new ObjectMapper();
        String json=objectMapper.writeValueAsString(user);
        printWriter.println(json);
        printWriter.flush();
        printWriter.close();
//        return "search";
    }

    @RequestMapping("/findUserById_nav")
    public String findUserById_nav() {
        return "search";
    }
}
